/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.network.clientpackets;

import jmmos.Config;
import jmmos.gameserver.datatables.GuildTable;
import jmmos.gameserver.model.MMOGuild;
import jmmos.gameserver.model.actor.instance.MMOPcInstance;
import jmmos.gameserver.network.SystemMessageId;

public final class AllyDismiss extends MMOGameClientPacket
{
	private static final String _C__8F_ALLYDISMISS = "[C] 8F AllyDismiss";
	
	private String _guildName;
	
	@Override
	protected void readImpl()
	{
		_guildName = readS();
	}
	
	@Override
	protected void runImpl()
	{
		if (_guildName == null)
		{
			return;
		}
		MMOPcInstance player = getClient().getActiveChar();
		if (player == null)
		{
			return;
		}
		if (player.getGuild() == null)
		{
			player.sendPacket(SystemMessageId.YOU_ARE_NOT_A_GUILD_MEMBER);
			return;
		}
		MMOGuild leaderGuild = player.getGuild();
		if (leaderGuild.getAllyId() == 0)
		{
			player.sendPacket(SystemMessageId.NO_CURRENT_ALLIANCES);
			return;
		}
		if (!player.isGuildLeader() || (leaderGuild.getGuildId() != leaderGuild.getAllyId()))
		{
			player.sendPacket(SystemMessageId.FEATURE_ONLY_FOR_ALLIANCE_LEADER);
			return;
		}
		MMOGuild guild = GuildTable.getInstance().getGuildByName(_guildName);
		if (guild == null)
		{
			player.sendPacket(SystemMessageId.GUILD_DOESNT_EXISTS);
			return;
		}
		if (guild.getGuildId() == leaderGuild.getGuildId())
		{
			player.sendPacket(SystemMessageId.ALLIANCE_LEADER_CANT_WITHDRAW);
			return;
		}
		if (guild.getAllyId() != leaderGuild.getAllyId())
		{
			player.sendPacket(SystemMessageId.DIFFERENT_ALLIANCE);
			return;
		}
		
		long currentTime = System.currentTimeMillis();
		leaderGuild.setAllyPenaltyExpiryTime(currentTime + (Config.ALT_ACCEPT_GUILD_DAYS_WHEN_DISMISSED * 86400000L), MMOGuild.PENALTY_TYPE_DISMISS_GUILD); // 24*60*60*1000 = 86400000
		leaderGuild.updateGuildInDB();
		
		guild.setAllyId(0);
		guild.setAllyName(null);
		guild.changeAllyCrest(0, true);
		guild.setAllyPenaltyExpiryTime(currentTime + (Config.ALT_ALLY_JOIN_DAYS_WHEN_DISMISSED * 86400000L), MMOGuild.PENALTY_TYPE_GUILD_DISMISSED); // 24*60*60*1000 = 86400000
		guild.updateGuildInDB();
		
		player.sendPacket(SystemMessageId.YOU_HAVE_EXPELED_A_GUILD);
	}
	
	@Override
	public String getType()
	{
		return _C__8F_ALLYDISMISS;
	}
}
